package day7;

import java.util.Stack;

// https://www.nowcoder.com/questionTerminal/d8acfa0619814b2d98f12c071aef20d4
public class code1 {
    public static void main(String[] args) {

    }
    public boolean chkParenthesis(String A, int n) {
        if(n == 0 || n % 2 != 0) {
            return false;
        }
        Stack<Character> stack = new Stack<>();
        for (int i = 0;i < n;i++) {
            if(A.charAt(i) == '(') {
                stack.push(A.charAt(i));
            }else if(A.charAt(i) == ')') {
                if(stack.isEmpty()) {
                    return false;
                }else if (stack.peek() == '('){
                    stack.pop();
                }
            }else {
                return false;
            }
        }
        if(stack.isEmpty()) {
            return true;
        }else {
            return false;
        }
    }
}
